from django.db.models import *
from django.contrib.auth.models import User



	
	
#===============================================================================
# Stock: stock information
#===============================================================================
class Stock(Model):
	stock_name = CharField(unique = True, max_length = 100)
	stock_companyname = CharField(unique = True, max_length = 100)
	def _unicode_(self):
		return self.stock_name
	
	

class UserAccount(Model):
	user = OneToOneField(User,related_name = 'related_user')
	credit = FloatField( max_length = 100, default = '10000000')
	image = ImageField(upload_to='img/profileimg/' , default = 'ucl2.jpg')
	def _unicode_(self):
		return self.credit
	



#===============================================================================
# News: Stock news
#===============================================================================
class News(Model):
	stock = ForeignKey(Stock,related_name = 'stock_related')
	news_content = TextField(max_length=5000)
	date = DateTimeField(auto_now_add=True)
	link = URLField(unique = True)
	def _unicode_(self):
		return self.pk



class AbstractRating(Model):
	TYPES = (('1', 'Not Satisfying'),
             ('2', 'Satisfying'),
             ('3', 'Good'),
             ('4', 'Very Good'),
             ('5', 'Excellent'))

	user = ForeignKey(User, related_name = '%(class)ss')
	rating = CharField(max_length = 1, choices = TYPES)

	class Meta:
		abstract = True



#===============================================================================
# NewsRating: user can rate the news
#===============================================================================
class NewsRating(AbstractRating):#http://www.djangosnippets.org/snippets/494/
	news = ForeignKey(News , related_name = '%(class)s_rating')
	class Meta:
		unique_together = (("news","user"))


class AbstractRec(Model):
    user = ForeignKey(User, related_name = '%(class)s_related')
    TYPES = (('SB', 'Strong recommend to buy'),
			 ('B', 'Recommend to buy'),
             ('H', 'Recommend to hold'),
             ('S', 'Recommend to sell'),
             ('SS', 'Strong recommend to sell'),)
    type = CharField(max_length=1, choices=TYPES) #TODO >0
    text = TextField(max_length=5000)
    class Meta:
        abstract = True




class FriendShip(Model):
	from_friend = ForeignKey(User, related_name='friend_set')
	to_friend = ForeignKey(User, related_name='to_friend_set')
	confirmed = BooleanField(default = False)
	def __unicode__(self):
		return  u'%s, %s' % (self.from_friend.username, self.to_friend.username)
	class Meta:
		permissions = (('can_list_friend_bookmarks','Can list friend bookmarks'),)
		unique_together = (('to_friend', 'from_friend'), )

